package com.yx.cloud.adapter;


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainAdapter extends FragmentPagerAdapter {
    private List<Fragment> mList = new ArrayList<>();

    public MainAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        mList = list;
    }

    public void updateData(List<Fragment> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void addItem(int index, Fragment fragment) {
        mList.add(index, fragment);
        notifyDataSetChanged();
    }

    public void deleteItem(int index) {
        mList.remove(index);
        notifyDataSetChanged();
    }

//    @Override
//    public void destroyItem(ViewGroup container, int position, Object object) {
//        notifyDataSetChanged();
//    }

    @Override
    public Fragment getItem(int position) {
        return mList.get(position);
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public int getItemPosition(Object object) {
//        if (object instanceof Fragment) {
//            int index = mList.indexOf(object);
//            if (index != -1) {
//                return index;
//            } else {
//                return POSITION_NONE;
//            }
//        }
        return POSITION_NONE;//每个item都重新刷新加载数据
    }

    @Override
    public long getItemId(int position) {
        return mList.get(position).hashCode();
    }
}
